<template>
  <view class="page">
    <view class="address-input">
      <view class="list-input">
        <view class="title">
          <text>收货人</text>
        </view>
        <view class="content">
          <input type="text"  v-model="addressinfo.name" placeholder="请填写收货人姓名">
        </view>
      </view>
      <view class="list-input">
        <view class="title">
          <text>手机号</text>
        </view>
        <view class="content">
          <input type="tel" v-model="addressinfo.phone" placeholder="请填写收货人手机号">
        </view>
      </view>
      <view class="list-input">
        <view class="title">
          <text>所在地区</text>
        </view>
        <view class="content">
          <input type="tel" v-model="addressinfo.region" placeholder="省市区县/乡镇等">
        </view>
      </view>
      <view class="list-textarea">
        <view class="title">
          <text>详细地址</text>
        </view>
        <view class="content">
          <textarea type="tel" v-model="addressinfo.address" placeholder="街道/楼牌号等" />
        </view>
      </view>
    </view>
    <view class="tag-default">
      <view class="tag-list">
        <view class="title">
          <text>标签</text>
        </view>
        <view class="content">
          <view @click="action('家')" :class="{'action': addressinfo.title == '家'}" class="list">
            <text>家</text>
          </view>
          <view @click="action('公司')" :class="{'action': addressinfo.title === '公司'}" class="list">
            <text>公司</text>
          </view>
          <view @click="action('学校')" :class="{'action': addressinfo.title == '学校'}" class="list">
            <text>学校</text>
          </view>
        </view>
      </view>
      <view class="default-address">
        <view class="title">
          <text>默认地址</text>
        </view>
        <view class="switch-default">
          <switch @change="chengeIsDefault(addressinfo.isDefault)" type="checkbox"
            :checked="addressinfo.isDefault>0" class="red sm" color="#0077EE !important"></switch>
        </view>
      </view>
    </view>
    <view class="footer-btn" @click="onSave">
      <view class="btn">
        <text>保存</text>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        addressType: '2',
        addressinfo: {
          id: '',
          userId:'',
          name: '',
          phone: '',
          region: '',
          address: '',
          title: '',
          isDefault: ''
        },

      }
    },
    onLoad(params) {
      this.addressType = params.type || '2';
      if (this.addressType == '1') {
        this.addressinfo.id = params.id;
        this.addressinfo.userId=this.$queue.getData("id");
        this.addressinfo.name = params.name;
        this.addressinfo.phone = params.phone;
        this.addressinfo.region = params.region;
        this.addressinfo.address = params.address;
        this.addressinfo.title = params.title;
        this.addressinfo.isDefault = params.isDefault;
      }

      uni.setNavigationBarTitle({
        title: this.addressType === '1' ? '编辑收货地址' : '新建收货地址'
      })

    },
    methods: {
      chengeIsDefault(defa) {
        if (defa > 0) {
          this.addressinfo.isDefault = 0
        } else {
          this.addressinfo.isDefault = 1

        }
      },
      action(title) {
        this.addressinfo.title = title
      },
      onSave() {
        
        if (this.addressType == 1) {
          uni.request({
        
            data: JSON.stringify(this.addressinfo),
            method: 'POST',
            url: "http://localhost:8089/cgoAdmin/updateAddressList",
            success: (res) => {
              if (res.data.success) {
                this.$queue.showToast("修改成功")
                uni.navigateBack({
                  delta:1
                })
              } else {
                this.$queue.showToast(res.data.message)
              }
            }
          });
          
        }else{
          uni.request({
            data: JSON.stringify(this.addressinfo),
            method: 'POST',
            url: "http://localhost:8089/cgoAdmin/insertAddressList",
            success: (res) => {
              if (res.data.success) {
                this.$queue.showToast("修改成功")
              
              } else {
                this.$queue.showToast(res.data.message)
              }
            }
          });
        }
        

      }
    }
  }
</script>

<style scoped lang="scss">
  @import 'AddressEdit.scss';
</style>